package com.lib.common.views;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lib.commom.R;
import com.lib.common.base.BaseActivity;

/**
 * @Author rames
 * @date 2018/11/21 21:20
 */
public class TitleView extends LinearLayout {
    private TextView titleTV, rightTV;
    private ImageView rightImg, backImg;

    private void init(){
        LayoutInflater.from(getContext()).inflate(R.layout.view_title_layout, this);
        backImg = this.findViewById(R.id.left_img);
        titleTV = this.findViewById(R.id.title_text);
        rightImg = findViewById(R.id.right_img);
        rightTV = findViewById(R.id.right_text);
        titleTV.setMaxLines(1);
        titleTV.setSingleLine();
        titleTV.setEllipsize(TextUtils.TruncateAt.END);

        backImg.setOnClickListener(v -> ((Activity)getContext()).finish());
    }

    public void setTitle(String title){
        titleTV.setText(title);
    }

    public void setRight(String content){
        rightTV.setText(content);
        rightTV.setVisibility(VISIBLE);
        rightImg.setVisibility(GONE);
    }

    public TextView getTitleTV() {
        return titleTV;
    }

    public void setRightImg(int sourceID){
        rightImg.setImageResource(sourceID);
        rightTV.setVisibility(INVISIBLE);
        rightImg.setVisibility(VISIBLE);
    }

    public ImageView getBackImg() {
        return backImg;
    }

    public TextView getRightTV(){
        return rightTV;
    }

    public TitleView(Context context) {
        super(context);
        init();
    }

    public TitleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TitleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void setRightItemClick(OnClickListener onClickListener){
        if(rightImg.getVisibility() == VISIBLE){
            rightImg.setOnClickListener(onClickListener);
        }else if(rightTV.getVisibility() == VISIBLE){
            rightTV.setOnClickListener(onClickListener);
        }
    }
}

